$(function() {
  $(".login-form").bootstrapValidator({
    excluded: [":disabled", ":hidden", ":not(:visible)"],
    feedbackIcons: {
      valid: "glyphicon glyphicon-ok",
      invalid: "glyphicon glyphicon-remove",
      validating: "glyphicon glyphicon-refresh"
    },
    fields: {
      username: {
        validators: {
          notEmpty: {
            message: "用户名不能为空"
          },
          stringLength: {
            min: 2,
            max: 6,
            message: "用户名长度必须在2到6位之间"
          },
          callback: {
            message: "用户名不存在"
          }
        }
      },
      password: {
        validators: {
          notEmpty: {
            message: "密码不能为空"
          },
          stringLength: {
            min: 6,
            max: 12,
            message: "密码长度必须在6到12位之间"
          },
          callback: {
            message: "密码错误！"
          }
        }
      }
    }
  });
  $(".btn-reset").click(function() {
    $(".login-form")
      .data("bootstrapValidator")
      .resetForm();
  });
  $(".login-form").on("success.form.bv", function(e) {
    e.preventDefault();
    //使用ajax提交逻辑
    $.ajax({
      type: "post",
      url: "/employee/employeeLogin",
      data: $(".login-form").serialize(),
      dataType: "json",
      success: function(res) {
        console.log(res);

        if (res.success) {
          location.href = "index.html";
        }
        if (res.error === 1000) {
          $(".login-form")
            .data("bootstrapValidator")
            .updateStatus("username", "INVALID", "callback");
        }
        if (res.error === 1001) {
          $(".login-form")
            .data("bootstrapValidator")
            .updateStatus("password", "INVALID", "callback");
        }
      }
    });
  });
});
